/*
   @Copyright:LeetCode
   @Author:   tjyemail
   @Problem:  http://leetcode.com/problems/happy-number
   @Language: C++
   @Datetime: 20-01-06 17:09
   */

// Method 1
class Solution {
public:
	bool isHappy(int n) {
		int s=0;
		for(unordered_set<int> dict; !dict.count(n); n=s){
			dict.insert(n);
			for(s=0; n; s+=(n%10)*(n%10), n/=10);
			if(s==1) return true;
		}
		return false;
	}
};

// Method 2, any unhappy number must enter 4->16->37->58->89->145->42->20->4...
class Solution {
public:
	bool isHappy(int n) {
		for(int s=0; n!=4; n=s){
			for(s=0; n; s+=(n%10)*(n%10), n/=10);
			if(s==1) return true;
		}
		return false;
	}
};

